package org.jvnet.hudson.client_demo;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.tmatesoft.svn.core.ISVNDirEntryHandler;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;

public class GetEncapName {

	public static void main(String[] args) throws Exception {

		final String usr = "dpssvn01";
		final String pwd = "abcd1234";
		final String urlt = "http://10.144.136.56/svndps_test/dpssvn01/MOrderSource/pom.xml";
		final String modId = "MOrderSource";

		Map<String, Object> map = new HashMap<String, Object>();
		InputStream res = null;

		String returnCode = "";

		try {
			res = URLConnectionUtil.doGet(urlt, true, usr, pwd, map);

			SAXReader reader = new SAXReader();
			Document document = reader.read(res);
			Element root = document.getRootElement();

			Iterator<Element> it = root.elementIterator();
			String fileName = "";
			String extName = "";
			while (it.hasNext()) {
				Element e = it.next();
				if ("name".equalsIgnoreCase(e.getName())) {
					fileName = e.getText();
				} else if ("packaging".equalsIgnoreCase(e.getName())) {
					extName = e.getText();
				}
				
				if (!"".equalsIgnoreCase(fileName) && !"".equalsIgnoreCase(extName)) {
					returnCode = fileName + "." + extName;
					break;
				}
			}

			

		} catch (FileNotFoundException e) {
			System.out.println("該夾內不存在pom.xml");
		} finally {
			if (res !=null)res.close();
		}

		if ("".equalsIgnoreCase(returnCode)) {
			System.out.println("POM沒有所要內容");
		}
		System.out.println("實際的值是：" + returnCode);
	}

}

/**
 * 存取SVN所需要的Class <br>
 * 創建時間：2013/9/24 下午6:44:15<br>
 * 創建人員：郭啟揚 (842297)
 */
class DirEntryHandler implements ISVNDirEntryHandler {
	public void handleDirEntry(SVNDirEntry dirEntry) throws SVNException {
		if (dirEntry.getKind() == SVNNodeKind.FILE) { // 僅有目錄方能被選擇

		}
	}
}